package org.fuxia.im.gui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import org.fuxia.im.common.ClientContext;
import org.fuxia.im.core.entries.ECPacket;
import org.tony.common.ui.CommonFrame;

public class GroupApplyHandler extends CommonFrame {
	
	private static final long serialVersionUID = 1L;

	public GroupApplyHandler() {
		super() ;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
		setContent(new GroupMemberApply()) ;
	}
	
	public void insertApplyMessage(String userInfo,String groupInfo) {
		if(this.getContent()!= null) {
			GroupMemberApply gma = (GroupMemberApply)this.getContent() ;
			gma.insertGroupInfo(userInfo, groupInfo) ;
		}
	}
	
	public void insertExtraInfo(String extraInfo){
		if(this.getContent()!= null) {
			GroupMemberApply gma = (GroupMemberApply)this.getContent() ;
			gma.insertExtraInfo(extraInfo) ;
		}
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GroupApplyHandler gma = new GroupApplyHandler() ;
					gma.setBounds(100, 100, 346, 285);
					gma.setResizable(false);
					gma.insertApplyMessage("这是测试用户(422605589)","这是测试群组(201212121314)"	) ;
					gma.insertExtraInfo("群组同意吧") ;
					gma.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	class GroupMemberApply extends JPanel   implements ActionListener{

		private static final long serialVersionUID = 1L;
		private JLabel groupIcon;
		private JLabel extraInfoLab;
		private JTextPane applyMessage;
		private JTextPane applyInfo;
		private JRadioButton agreeRadio;
		private JRadioButton rejectRadio;
		private JTextField rejectReason;
		private JPanel panel;
		private JButton agreeBtn;
		private JButton cancel;
		private Font comonFont ;
		
		private boolean isAgree ;

		public GroupMemberApply() {
			comonFont = new Font("微软雅黑", Font.PLAIN, 12) ;
			initialize();
		}
		private void initialize() {
			setLayout(null);
			
			groupIcon = new JLabel("");
			groupIcon.setIcon(new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("images/tangling.jpg"))) ;
			groupIcon.setBounds(10, 18, 64, 64);
			add(groupIcon);
			
			extraInfoLab = new JLabel("\u9644\u52A0\u4FE1\u606F:");
			extraInfoLab.setFont(comonFont);
			extraInfoLab.setBounds(86, 62, 109, 18);
			add(extraInfoLab);
			
			applyMessage = new JTextPane();
			applyMessage.setBounds(83, 83, 261, 54);
			applyMessage.setOpaque(false);
			applyMessage.setEditable(false);
			applyMessage.setFont(comonFont);
			applyMessage.setForeground(Color.BLACK) ;
			applyMessage.setBorder(null);
			add(applyMessage);
			
			applyInfo = new JTextPane();
			applyInfo.setFont(comonFont);
			applyInfo.setEditable(false);
			applyInfo.setOpaque(false);
			applyInfo.setBounds(83, 18, 261, 45);
			applyInfo.setBorder(null);
			add(applyInfo);
			
			agreeRadio = new JRadioButton("\u540C\u610F");
			agreeRadio.setFont(comonFont);
			agreeRadio.setBounds(83, 149, 53, 18);
			agreeRadio.setOpaque(false);
			agreeRadio.addActionListener(this);
			add(agreeRadio);
			
			rejectRadio = new JRadioButton("\u62D2\u7EDD");
			rejectRadio.setFont(comonFont);
			rejectRadio.setBounds(139, 149, 53, 18);
			rejectRadio.setOpaque(false);
			add(rejectRadio);
			rejectRadio.addActionListener(this);
			
			rejectReason = new JTextField();
			rejectReason.setVisible(false);
			rejectReason.setBounds(83, 179, 260, 30);
			rejectReason.setFont(comonFont);
			/*rejectReason.addFocusListener(new FocusListener() {
				
				public void focusLost(FocusEvent e) {
					GroupApplyHandler.this.repaint() ;
				}
				
				public void focusGained(FocusEvent e) {
					GroupApplyHandler.this.repaint() ;
				}
			});*/
			/**rejectReason.addKeyListener(new KeyAdapter() {

				public void keyTyped(KeyEvent e) {
					doUpdate(e);
				}

				public void keyPressed(KeyEvent e) {
					doUpdate(e);
				}
				public void keyReleased(KeyEvent e) {
					doUpdate(e);
				}
				
				private void doUpdate(KeyEvent e){
					if(!hasRepainted) {
						GroupApplyHandler.this.repaint();
						hasRepainted = true ;
					}
					
				}
				
			});
			/*rejectReason.getDocument().addDocumentListener(new DocumentListener() {
				
				public void removeUpdate(DocumentEvent e) {
					
				}
				
				public void insertUpdate(DocumentEvent e) {
					GroupApplyHandler.this.update(GroupApplyHandler.this.getGraphics()) ;
				}
				
				public void changedUpdate(DocumentEvent e) {
					System.out.println("aaa");
				}
			});*/
			add(rejectReason);
			

			ButtonGroup bg = new ButtonGroup();// 按三个单选按钮划分到一个按钮组中
			bg.add(rejectRadio);
			bg.add(agreeRadio);
			rejectReason.setColumns(10);
			
			
			panel = new JPanel();
			panel.setBorder(null);
			panel.setOpaque(false);
			panel.setBounds(0, 238, 346, 31);
			panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0)) ;
			add(panel);
			
			agreeBtn = new JButton("   确定  ");
			agreeBtn.setFont(comonFont);
			agreeBtn.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent e) {
					ECPacket packet = GroupApplyHandler.this.getPropertiy("packet", ECPacket.class) ;
					packet.setFrom(ClientContext.getLoginUser().getUserCode());
					packet.addParam("isAgree", String.valueOf(isAgree)) ;
					try {
						ECPacket p = getPropertiy("packet", ECPacket.class) ;
 						ClientContext.addUserInChatGroup(p.getParameter("groupId"),
 								p.getParameter("userCode"), p.getParameter("nickName"));
						ClientContext.getContext().getClient().sendMessage(packet);
						hanlderCloseEvent() ;
					} catch (Exception e1) {
						//no ops
					}
				}
			});
			panel.add(agreeBtn);
			
			cancel = new JButton("   取消   ");
			cancel.setFont(comonFont);
			panel.add(cancel);
			cancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					GroupApplyHandler.this.hanlderCloseEvent() ;
				}
			});
			
		}
		
		private void insertGroupInfo(String userInfo,String grouInfo) {
			this.applyInfo.setText("") ;
			SimpleAttributeSet attrSet = new SimpleAttributeSet();
			StyleConstants.setForeground(attrSet, Color.BLUE);
			StyleConstants.setUnderline(attrSet, true);
			insert(this.applyInfo,userInfo,attrSet);
			
			StyleConstants.setForeground(attrSet, Color.BLACK);
			StyleConstants.setUnderline(attrSet, false);
			insert(this.applyInfo," 申请加入群 ",attrSet);
			
			StyleConstants.setForeground(attrSet, Color.BLUE);
			StyleConstants.setUnderline(attrSet, true);
			insert(this.applyInfo,grouInfo,attrSet);
		}
		
		private void insertExtraInfo(String info) {
			this.applyMessage.setText(info);
		}
		
		private void insert(JEditorPane editorPane, String str, AttributeSet asst) {
			int lenth = editorPane.getDocument().getLength();
			try {
				editorPane.getDocument().insertString(lenth, str, asst);
				editorPane.setCaretPosition(editorPane.getDocument().getLength());
				editorPane.repaint() ;
			} catch (Exception e) {
				// no ops
			}
		}
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == agreeRadio) {
				isAgree = true ;
				rejectReason.setVisible(false);
			}else if(e.getSource() == rejectRadio) {
				isAgree = false ;
				rejectReason.setVisible(true);
			}
		}
	}

}
